﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SW.Lite.BLL;
using SW.Lite.Core.Attributes;
using SW.Lite.Core.Authority;
using SW.Lite.Infrastructure.Common;
using SW.Lite.Infrastructure.Helpers;
using SW.Lite.Model;

namespace SW.Lite.WebSite.Areas.Master.Controllers
{
    /// <summary>
    /// Class ColumnController.
    /// </summary>
    public class ColumnController : BaseController
    {
        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Menu(MenuCode = "ColumnManage")]
        public ActionResult Index()
        {
            var list = Column.All().ToList();
            list.Add(new ColumnModel
            {
                Id = 0,
                ParentID = 0,
                Title = "栏目列表",
                Code = "",
                Sort = -1
            });

            list.Sort(new ColumnComparer());
            var result = list.Select(n => new
            {
                id = n.Id.ToString(),
                pid = n.ParentID == 0 ? n.ParentID.ToString() : Guid.Empty.ToString(),
                name = n.Title,
                n.Code,
                open = true
            });
            ViewBag.Tree = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            var models = BLL.Model.All().ToList();
            ViewBag.Models = models;
            return View();
        }

        /// <summary>
        /// Gets the columns.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>JsonResult.</returns>
        [Menu(MenuCode = "ColumnManage")]
        [HttpPost]
        public JsonResult GetColumns(int id)
        {
            if (id != 0)
            {
                var children = Column.GetChildren(id);
                children.Sort(new ColumnComparer());
                var result = children.Select(n => new
                {
                    ColumnID = n.Id,
                    n.Code,
                    n.Title,
                    n.Type,
                    n.NavigateUrl,
                    n.ModelCode,
                    n.Sort
                });
                return new JsonResult { Data = result };
            }
            else
            {
                var columns = Column.GetTops();
                columns.Sort(new ColumnComparer());
                var result = columns.Select(n => new
                {
                    ColumnID = n.Id,
                    n.Code,
                    n.Title,
                    n.Type,
                    n.NavigateUrl,
                    n.ModelCode,
                    n.Sort
                });
                return new JsonResult { Data = result };
            }
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <returns>JsonResult.</returns>
        [HttpPost]
        [Button(MenuCode = "ColumnManage", ButtonCode = "Save", IsAjax = true)]
        public JsonResult Save()
        {
            var sorts = Newtonsoft.Json.JsonConvert.DeserializeObject<List<KeyValuePair<int, int>>>(RequestHelper.Instance.FormString("sorts"));

            var columnSorts = new Dictionary<int, int>();
            sorts.ForEach(kv => columnSorts.Add(kv.Key, kv.Value));
            var result = Column.SaveSorts(columnSorts);
            return result ? new JsonResult { Data = new ExecuteResult { Result = true } } : new JsonResult { Data = new ExecuteResult { Result = false, Message = "保存失败" } };
        }

        [HttpPost]
        [Button(MenuCode = "ColumnManage", ButtonCode = "Add", IsAjax = true)]
        public JsonResult Add(ColumnModel col)
        {
            var model = BLL.Model.Single(col.ModelId);

            if (model == null)
                return new JsonResult { Data = new ExecuteResult { Result = false, Message = "模型不存在" } };

            col.ModelCode = model.Code;

            var result = Column.Insert(col);

            return result
                ? new JsonResult { Data = new ExecuteResult { Result = true } }
                : new JsonResult { Data = new ExecuteResult { Result = false, Message = result.Message } };
        }
    }
}